/*
 * @lc app=leetcode.cn id=653 lang=cpp
 *
 * [653] 两数之和 IV - 输入 BST
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
  bool findTarget(TreeNode* root, int k)
  {
    vector<int> v;
    Inorder(v, root);
    int L = 0, R = v.size() - 1;
    while (L < R) {
      if (v[L] + v[R] == k)
        return true;
      else if (v[L] + v[R] < k)
        ++L;
      else
        --R;
    }
    return false;
  }

  void Inorder(vector<int>& v, TreeNode* root)
  {
    if (root) {
      Inorder(v, root->left);
      v.push_back(root->val);
      Inorder(v, root->right);
    }
  }
};
// @lc code=end
